package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.*;
import cn.edu.scau.dropship.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private WishlistService wishlistService;
    @Autowired
    private WishGoodsService wishGoodsService;
    @Autowired
    private StoreGoodsService storeGoodsService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ClassificationService classificationService;


    //获取所有上架商品
    @PostMapping(value = "/getAllGroudingGoods")
    @ResponseBody
    public List<Goods> getAllGroudingGoods(HttpServletRequest request) {
        List<Goods> list=goodsService.selectByState("A");
        return list;
    }

    //添加商品到心愿单
    @PostMapping(value = "/goodsToWish")
    @ResponseBody
    public String goodsToWish(HttpServletRequest request) {
        Integer uid=Integer.valueOf(request.getParameter("uid"));
        //根据uid遍历wishlist，是否已存在心愿单
        Wish_list wish_list=wishlistService.selectByUid(uid);
        if(wish_list==null){
            Wish_list wish_list1=new Wish_list();
            wish_list1.setId(uid);
            wish_list1.setBvoId(uid);
            wishlistService.insert(wish_list1);
        }
        Integer gid=Integer.valueOf(request.getParameter("gid"));
        Wish_goodsKey wish_goodsKey=new Wish_goodsKey();
        wish_goodsKey.setGoodsId(gid);
        wish_goodsKey.setWishId(uid);
        int result=wishGoodsService.insert(wish_goodsKey);
        if(result>0){
            return "添加到心愿单成功";
        }
        return "添加到心愿单失败";
    }

    //添加商品到店铺
    @PostMapping(value = "/goodsToStore")
    @ResponseBody
    public String goodsToStore(HttpServletRequest request){
        Integer gid=Integer.valueOf(request.getParameter("gid"));
        Integer sid=Integer.valueOf(request.getParameter("sid"));
        Store_goods store_goods=new Store_goods();
        store_goods.setGoodsid(gid);
        store_goods.setStoreid(sid);
        int result=storeGoodsService.insert(store_goods);
        if(result<=0){
            return "添加到店铺失败";
        }
        return "添加到店铺成功";
    }








    //gid找商品
    @PostMapping("/searchGoodsByID")
    @ResponseBody
    public Goods searchGoodsByID(Integer id) {
        return goodsService.selectByPrimaryKey(id);
    }

    /*public int insertGoodsProfilePhoto(Integer image) {
        return 1;
    }*/

    @PostMapping(value = "/addGoods")
    @ResponseBody
    public String addGoods(String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String brandclassificationname, String brandname) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods goods = new Goods(title , length , width , height , weight , sku , upc , ena , model , price , warrantly_time , ebay_description , amazon_description ,  classification.getId() , brand.getId() );
        goods.setState("S");
        int re = goodsService.insert(goods);
        if (re == 1) return "添加商品讯息成功";
        return "添加商品讯息失败";
    }

    @PostMapping(value = "/addGoodsWithPic")
    @ResponseBody
    public String addGoods(String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String brandclassificationname, String brandname, String image) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods goods = new Goods(title, length, width, height, weight, sku, upc, ena, model, price, warrantly_time, ebay_description, amazon_description, image, classification.getId(), brand.getId());
        goods.setState("S");

        System.out.println(image);
        System.out.println(goods.getImage());


        int re = goodsService.insert(goods);
        if (re == 1) return "添加商品讯息成功";
        return "添加商品讯息失败";
    }

    @PostMapping("/selectGoodsByTitle")
    @ResponseBody
    public Goods selectGoodsByTitle(String title) {
        return goodsService.selectByTitle(title);
    }

    @PostMapping("/deleteGoods")
    @ResponseBody
    public String deleteGoods(Integer id) {
        int re = goodsService.deleteByPrimaryKey(id);
        if (re == 1) {
            System.out.println("删除商品成功");
            return "删除商品成功";
        }
        System.out.println("删除商品失败");
        return "删除商品失败";
    }

    @PostMapping(value = "/getAllGoodsByBvoId")
    public List<Goods> getAllGoods(Integer brandId) {
        return goodsService.selectAllByBrandId(brandId);
    }

    @PostMapping("/updateGoodsWithPic")
    @ResponseBody
    public String updateGoods(Integer id, String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String image, String state, String brandclassificationname, String brandname) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods record = new Goods(id, title, length, width, height, weight, sku, upc, ena, model, price, warrantly_time, ebay_description, amazon_description, image, state, classification.getId(), brand.getId());
        record.setState("S");
        int re = goodsService.updateByPrimaryKey(record);
        if (re == 1) return "更新商品讯息成功";
        return "更新商品讯息失败";
    }

    @PostMapping("/updateGoods")
    @ResponseBody
    public String updateGoods(Integer id, String title, Double length, Double width, Double height, Double weight, String sku, String upc, String ena, String model, Double price, String warrantly_time, String ebay_description, String amazon_description, String state, String brandclassificationname, String brandname) {
        Classification classification = classificationService.selectByName(brandclassificationname);
        Brand brand = brandService.selectByName(brandname);
        Goods record = new Goods(id, title, length, width, height, weight, sku, upc, ena, model, price, warrantly_time, ebay_description, amazon_description, state, classification.getId(), brand.getId());
        record.setState("S");
        int re = goodsService.updateByPrimaryKey(record);
        if (re == 1) return "更新商品讯息成功";
        return "更新商品讯息失败";
    }

    @PostMapping("/uploadToStorehouse")
    @ResponseBody
    public String uploadToStorehouse(Integer id) {
        int re = goodsService.uploadToStorehouse(id);
        if (re == 1) return "商品成功入仓";
        return "商品入仓失败, 此商品并没有录入";
    }

    @PostMapping("/uploadToStore")
    @ResponseBody
    public String uploadToStore(Integer id) {

        int re = goodsService.uploadToStore(id);
        if (re == 1) return "商品成功上架";
        return "商品上架失败, 仓库不存在此商品";
    }

    @PostMapping("/goOffFromStore")
    @ResponseBody
    public String goOffFromStore(Integer id) {
        int re = goodsService.goOffFromStore(id);
        if (re == 1) return "商品成功下架";
        return "商品下架失败，商店不存在此商品";
    }

    @PostMapping("/offstorehouse")
    @ResponseBody
    public String offstorehouse(Integer id) {
        int re = goodsService.offstorehouse(id);
        if (re == 1) return "商品成功下仓";
        return "商品下仓失败，商店不存在此商品";
    }

    @PostMapping("/selectGoodsByCompanyID")
    @ResponseBody
    public List<Goods> selectGoodsByCompanyID(Integer mvo_id) {
        return goodsService.selectGoodsByCompanyID(mvo_id);
    }
}
